package com.wuliu.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.wuliu.common.constant.OrderConstants;
import com.wuliu.common.core.domain.entity.SysAppletUser;
import com.wuliu.common.utils.DateUtils;

import javax.annotation.Resource;

import com.wuliu.common.utils.SecurityUtils;
import com.wuliu.system.domain.BasFiles;
import com.wuliu.system.domain.dto.app.TspProblemDto;
import com.wuliu.system.service.IBasFilesService;
import org.springframework.stereotype.Service;
import com.wuliu.system.mapper.TspProblemMapper;
import com.wuliu.system.domain.TspProblem;
import com.wuliu.system.service.ITspProblemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 问题件Service业务层处理
 *
 * @author realue
 * @date 2023-11-23
 */
@Service
public class TspProblemServiceImpl extends ServiceImpl<TspProblemMapper, TspProblem> implements ITspProblemService {
    @Resource
    private TspProblemMapper tspProblemMapper;
    @Resource
    private IBasFilesService basFilesService;

    /**
     * 查询问题件
     *
     * @param id 问题件主键
     * @return 问题件
     */
    @Override
    public TspProblem selectTspProblemById(String id) {
        return tspProblemMapper.selectTspProblemById(id);
    }

    /**
     * 查询问题件列表
     *
     * @param tspProblem 问题件
     * @return 问题件
     */
    @Override
    public List<TspProblem> selectTspProblemList(TspProblem tspProblem) {
        return tspProblemMapper.selectTspProblemList(tspProblem);
    }

    /**
     * 新增问题件
     *
     * @param tspProblem 问题件
     * @return 结果
     */
    @Override
    public int insertTspProblem(TspProblem tspProblem) {
        tspProblem.setCreateTime(DateUtils.getNowDate());
        return tspProblemMapper.insertTspProblem(tspProblem);
    }

    /**
     * 修改问题件
     *
     * @param tspProblem 问题件
     * @return 结果
     */
    @Override
    public int updateTspProblem(TspProblem tspProblem) {
        tspProblem.setUpdateTime(DateUtils.getNowDate());
        return tspProblemMapper.updateTspProblem(tspProblem);
    }

    /**
     * 批量删除问题件
     *
     * @param ids 需要删除的问题件主键
     * @return 结果
     */
    @Override
    public int deleteTspProblemByIds(String[] ids) {
        return tspProblemMapper.deleteTspProblemByIds(ids);
    }

    /**
     * 删除问题件信息
     *
     * @param id 问题件主键
     * @return 结果
     */
    @Override
    public int deleteTspProblemById(String id) {
        return tspProblemMapper.deleteTspProblemById(id);
    }

    /**
     * 新增问题件
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(List<TspProblemDto> dto) {
        SysAppletUser user = SecurityUtils.getAppletUser();
        List<BasFiles> basFiles = new ArrayList<>();
        List<TspProblem> tspProblems = dto.stream().map(t -> {
            TspProblem tspProblem = new TspProblem();
            BasFiles files = new BasFiles();
            BeanUtil.copyProperties(t, tspProblem);
            tspProblem.setId(IdUtil.simpleUUID());
            tspProblem.setCreateTime(DateUtils.getNowDate());
            tspProblem.setSysAccount(user.getSysAccount());
            tspProblem.setStatus(0);
            files.setId(IdUtil.simpleUUID());
            files.setRelationId(t.getOrderId());
            files.setFileUrl(t.getImageUrl());
            files.setType(1);
            files.setFileType(OrderConstants.ORDER_STATUS_12);
            files.setCreateTime(DateUtils.getNowDate());
            files.setSysAccount(user.getSysAccount());
            basFiles.add(files);
            return tspProblem;
        }).collect(Collectors.toList());
        basFilesService.saveBatch(basFiles);
        return this.saveBatch(tspProblems);
    }
}
